#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify,g ,make_response


from service.administratorService import AdministratorService
from controller.administratorController import AdministratorController
from utils import commons
from utils.response_code import RET
from utils.verify_operate import verify_user_role

class AdministratorOtherResource(Resource):

    # 系统用户登录接口
    @classmethod
    def login(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('administrator_account', type=str, location='form', required=True, help='账号参数类型不正确或缺失')
        parser.add_argument('administrator_password', type=str, location='form', required=True, help='密码参数类型不正确或缺失')
        kwargs = parser.parse_args()
        # 去除空值
        kwargs = commons.put_remove_none(**kwargs)
        # 数据处理
        result = AdministratorService.administrator_login(**kwargs)

        # 检查 result 是否是 Response 对象
        if isinstance(result, make_response(jsonify({})).__class__):
            result_dict = result.get_json()
        else:
            result_dict = result

        # 检查 code 是否为 '2000'
        if result_dict['code'] != '2000':
            return make_response(jsonify({'code': result_dict['code'], 'message': result_dict['message']}), 400)

        # 其他逻辑
        return result


        #
        # if result_dict['code'] != '2000':
        #     return {'code': result_dict['code'], 'message': result_dict['message']}
        # else:
        #     return {'code': result_dict['code'], 'message': result_dict['message'], 'data': result_dict['data']}

    # 新管理员注册
    @classmethod
    # @verify_user_role([2])
    def administrator_register(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('administrator_name', type=str, location='form', required=True,help='administrator_name参数类型不正确或缺失')
        parser.add_argument('administrator_account', type=str, location='form', required=True,help='administrator_id参数类型不正确或缺失')
        parser.add_argument('administrator_password', type=str, location='form', required=True,help='administrator_password参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        result = AdministratorService.administrator_register(**kwargs)
        return jsonify(code=RET.OK, message=result['message'], data=result['data'])


    # 管理员修改密码
    @classmethod
    def modify_password(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('old_password', type=str, location='form', required=True,
                            help='old_password参数类型不正确或缺失')
        parser.add_argument('new_password', type=str, location='form', required=True,
                            help='new_password参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['administrator_id'] = g.user.get('user_id')

        res = AdministratorService.modify_password(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])



    # 获取当前登录的管理员信息
    @classmethod
    @verify_user_role([1])
    def me(cls):
        kwargs = {
            'administrator_id': g.user.get('user_id')
        }
        res = AdministratorService.me(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])



    # 修改头像
    @classmethod
    @verify_user_role([1])
    def modify_headpic(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('administrator_head', type=str, location='form', required=True, help='head_pic参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        kwargs['administrator_id'] = g.user.get('user_id')
        res = AdministratorService.modify_headpic(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 逻辑删除普通用户
    @classmethod
    @verify_user_role([1])
    def logical_delete(cls, user_id):
        if not user_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        res = AdministratorService.logical_delete(user_id)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])



    @classmethod
    def sensitive_update(cls, administrator_id):
        if not administrator_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('is_delete', location='form', required=False, help='is_delete参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['administrator_id'] = administrator_id

        res = AdministratorController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
